package com.colorfruit.framework.common.util;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 数据转换类
 * 1. 金额转换 后端的金额会出现除不尽的情况,这种情况下数字在前端会展示一个很长的数,影响美观.
 * 2. 包装类转换
 * 3. 日期转换
 */
@Slf4j
public class DataConvertUtil {
    /**
     * 将double数字保留三位小数 ("0.000")
     * @param number 需要被转换的数字
     * @param format 保留的格式
     * @return
     */
    public static String doubleToString(double number,String format){
        DecimalFormat df = new DecimalFormat(format);
        String value = df.format(number);
        return value;
    }

    /**
     * 按四舍五入的规则 获得对应的值
     *
     * @param N 将要保留的小数位数
     * @param d 待按照N位小数进行四舍五入的数
     * @return
     */
    public static double getRoundUpValue(int N, double d) {
        double value = d;
        for (int i = 0; i < N; i++) {
            value *= 10;
        }
        value = Math.round(value);
        for (int i = 0; i < N; i++) {
            value /= 10;
        }
        return value;
    }

    public static int Integer2int(Integer number){
        if (number == null){
            return 0;
        }
        return number;
    }

    public static long Long2long(Long number){
        if (number == null){
            return 0;
        }
        return number;
    }

    public static byte Byte2byte(Byte number){
        if (number == null){
            return 0;
        }
        return number;
    }

    public static double Double2double(Double number){
        if (number == null){
            return 0;
        }
        return number;
    }

    public static double float2float(Float number){
        if (number == null){
            return 0;
        }
        return number;
    }

    /**
     * 去除*号
     * @param sourceString
     * @return
     */
    public static String removeXH(String sourceString){
        if (sourceString == null || "".equals(sourceString)){
            return "";
        }
        String s = sourceString.replaceAll("\\*", "");
        return  s;
    }

    /**
     * 字符串按照指定格式转换为日期类型
     *
     * @param dateString 需要转换的字符串
     * @param format     需要转换成的格式
     * @return
     */
    public static Date StringToDateWithFormat(String dateString, String format) {
        if (dateString == null || "".equals(dateString)){
            return null;
        }
        SimpleDateFormat sf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sf.parse(dateString);
        } catch (ParseException e) {
            log.error("format = {}, 解析失败, 尝试使用新的解析方式", format);
            SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                sf2.parse(dateString);
            }catch (Exception e1){
                log.error("format = yyyy-MM-dd HH:mm:ss, 解析失败, 尝试使用新的解析方式", format);
                Long integer = Long.valueOf(dateString);
                date = new Date(integer);
            }
        }
        return date;
    }

    public static String dataToStringWithFormat(Date date, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String afterDate = sdf.format(date);       //将Date类型转换成String类型
        return afterDate;
    }

    public static Date StringToDateWithFormatEnd(String dateString, String format) {
        if (dateString == null || "".equals(dateString)){
            return null;
        }
        SimpleDateFormat sf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sf.parse(dateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            Date time = calendar.getTime();
            return time;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取当前日期的开始时间
     * @param date
     * @return
     */
    public static Date getTheDayBegin(Date date){
        if (date == null){
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date time = calendar.getTime();
        return time;
    }

    /**
     * 获取num天前的一天开始时间
     * @param num
     * @return
     */
    public static Date getDayBeforStartTime(int num){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)-num );
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date time = calendar.getTime();
        return time;
    }


    /**
     * 获取当前日期的当天结束时间
     * @param date
     * @return
     */
    public static Date getTheDayEnd(Date date){
        if (date == null){
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date time = calendar.getTime();
        return time;
    }

    /**
     * 将前端传过来的字符串转换为long类型
     * @param s
     * @return
     */
    public static Long string2Long(String s){
        if (s == null){
            return null;
        }else if ("".equals(s)){
            return (long)0;
        }  else {
            return Long.valueOf(s);
        }
    }


    /**
     * 将字符串类型转换为整形
     * @param s
     * @return
     */
    public static Integer string2Integer(String s){
        if (s == null){
            return null;
        }else if ("".equals(s)){
            return 0;
        }  else {
            return Integer.valueOf(s);
        }
    }

    /**
     * 将字符串类型转换为byte类型
     * @param s
     * @return
     */
    public static Byte string2Byte(String s){
        if (s == null){
            return null;
        }else if ("".equals(s)){
            return (byte)0;
        }  else {
            return Byte.valueOf(s);
        }
    }


    /**
     *
     * @param s
     * @return
     */
    public static String  StringToNotNull (String s){
        if (s == null){
            return "";
        }
        return s;
    }


    public static BigDecimal intToBigdecimal(Integer num){
        if (num == null){
            return new BigDecimal(0);
        }
        String numString = "" + num;
        String result;
        char[] chars = numString.toCharArray();
        char[] charAfter = new char[chars.length+1];
        if (chars.length > 2){
            int offset = 0;
            for (int i = 0; i < chars.length; i++) {
                if (i == chars.length -2){
                    charAfter[i] = 46;
                    offset += 1;
                }
                charAfter[i+offset] = chars[i];
            }
            result = String.valueOf(charAfter);
        }else {
            result = "0" + "." + num;
        }
        System.out.println(result);
        return new BigDecimal(result);
    }

    public static BigDecimal bigDecimal2notNull(BigDecimal param){
        if (param == null){
            return new BigDecimal(0);
        }
        return param;
    }

    public static void   getAscl(double value){
        char[] chars = (value + "").toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.println((int)chars[i]);
        }
    }

    public static Long getMillisTime( int hour, int minute, int second){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR,1970 );
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1 );
        calendar.set(Calendar.HOUR_OF_DAY, hour + 8);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        long timeInMillis = calendar.getTimeInMillis();
        System.out.println(timeInMillis);
        return timeInMillis;
    }

    public static void main(String[] args) {
        getMillisTime(0,0,0);
//        getAscl(1.23);
    }

}
